home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c
- Subject: Re: How to give functions as parameters?
- Date: Wed, 31 Jan 1996 16:11:10 +0200
- Organization: Carelcomp Forest
- Message-ID: <310F787E.7B14@cmt.lpr.mail.carel.fi>
- References: <Pine.SUN.3.91N2x.960130222756.11581A-100000@yellow59.nada.kth.se>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
-
- Per Steneskog wrote:
- >
- > I have a little problem, I dont know how to send a fuction,
- > as a parameter to another function..
- >
- > #include <stdio.h>
- >
- > void
- > do_it(void (*it)())
-
- You are expecting a pointer to a function returning void...
-
- > {
- > ((*(it))());
-
- Too much to type, just use
- (*it)();
- > }
- >
- >
- > void
- > do_me(
- > void)
- > {
- > printf("Testing!");
- > }
- >
- > int
- > main(
- > void)
- > {
- > do_it(do_me()); /* ERROR-COMPILE LINE */
-
- You are calling do_it and passing to it the return value of do_me, which is
- nothing (first, do_me gets executed, the return value, none in this case, is then
- passed as a parameter to do_it). Change the line to read:
-
- do_it(do_me);
-
- which will get the address of do_me passing that to do_it. do_it will then
- correctly call do_me.
- > }
- >
- > Short explaination: From main(), do_it() should be called with
- > the parameter (and function) do_me(). (Pretty obvious :)
- >
-
- To make it clear, you cannot pass a function to a function. You _can_ pass the
- address (pointer) or the return value of a function.
-
- > When I try to compile this, i got the following error message: (gcc)
- > In function `main':
- > invalid use of void expression
- >
- > My question is, what should I write instead of that line in main()?
- >
- > THANKS everyone out there!
- >
-
- You're welcome.
-
- Later,
- AriL
- --
- All my opinions are mine and mine alone.
-